package com.heroh.kettle.util;

import com.heroh.kettle.constant.KettleConstant;
import org.pentaho.di.core.Const;
import org.pentaho.di.core.logging.*;
import org.pentaho.di.core.variables.VariableSpace;
import org.pentaho.di.core.variables.Variables;
import org.pentaho.di.trans.TransMeta;

/**
 *  转换日志
 *
 * @author jianshengxiong
 * @date 2019/12/31/15:45
 */
public class KettleTransLog {

    // 转换-转换日志表
    private static final String KETTLE_LOG_TRANS_TABLE = "param_kettle_log_trans";
    // 转换-步骤日志表
    private static final String KETTLE_LOG_TRANS_STEP_TABLE = "param_kettle_log_trans_step";
    // 转换-运行日志表
    private static final String KETTLE_LOG_TRANS_PERFORMANCE_TABLE = "param_kettle_log_trans_performance";
    // 转换-日志通道
    private static final String KETTLE_LOG_TRANS_CHANNEL_TABLE = "param_kettle_log_trans_channel";
    // 转换-性能监控
    private static final String KETTLE_LOG_TRANS_METRICS_TABLE = "param_kettle_log_trans_metrics";

    private static VariableSpace SPACE = null;

    private static void init(){
        // 将step日志数据库配置名加入到变量集中
        SPACE = new Variables();
        SPACE.setVariable(Const.KETTLE_TRANS_LOG_DB, KettleConstant.KETTLE_LOG_DB);
        SPACE.setVariable(Const.KETTLE_METRICS_LOG_DB, KettleConstant.KETTLE_LOG_DB);
        SPACE.initializeVariablesFrom(null);
    }

    public static void setTransLog(TransMeta transMeta) {
        transMeta.setLogLevel(LogLevel.DETAILED);
        init();
        // 设置转换日志表
        setTransLogTable(transMeta);
        // 设置步骤日志表
        setStepLogTable(transMeta);
        // 设置运行日志表
        setPerformanceLogTable(transMeta);
        // 设置日志通道表
        setChannelLogTable(transMeta);
        // 设置性能监控表
        setMetricsLogTable(transMeta);
    }


    /**
     *  设置转换日志表
     *
     * @author jianshengxiong
     * @date 2019/12/31/15:45
     */
    private static void setTransLogTable(TransMeta transMeta) {
        TransLogTable transLogTable = TransLogTable.getDefault(SPACE,transMeta,transMeta.getSelectedSteps());
        transLogTable.setConnectionName(KettleConstant.KETTLE_LOG_DB);
        transLogTable.setTableName(KETTLE_LOG_TRANS_TABLE);
        transMeta.setTransLogTable(transLogTable);
    }

    /**
     *  设置步骤日志表
     *
     * @author jianshengxiong
     * @date 2019/12/31/15:45
     */
    private static void setStepLogTable(TransMeta transMeta) {
        StepLogTable stepLogTable = StepLogTable.getDefault(SPACE, transMeta);
        stepLogTable.setConnectionName(KettleConstant.KETTLE_LOG_DB);
        stepLogTable.setTableName(KETTLE_LOG_TRANS_STEP_TABLE);
        transMeta.setStepLogTable(stepLogTable);
    }

    /**
     *  转换-运行日志表
     *
     * @author jianshengxiong
     * @date 2019/12/31/15:45
     */
    private static void setPerformanceLogTable(TransMeta transMeta){
        PerformanceLogTable performanceLogTable = PerformanceLogTable.getDefault(SPACE,transMeta);
        performanceLogTable.setConnectionName(KettleConstant.KETTLE_LOG_DB);
        performanceLogTable.setTableName(KETTLE_LOG_TRANS_PERFORMANCE_TABLE);
        performanceLogTable.setLogInterval("1000");
        transMeta.setPerformanceLogTable(performanceLogTable);
    }

    /**
     *  设置日志通道表
     *
     * @author jianshengxiong
     * @date 2019/12/31/15:45
     */
    private static void setChannelLogTable(TransMeta transMeta){
        ChannelLogTable channelLogTable = ChannelLogTable.getDefault(SPACE,transMeta);
        channelLogTable.setConnectionName(KettleConstant.KETTLE_LOG_DB);
        channelLogTable.setTableName(KETTLE_LOG_TRANS_CHANNEL_TABLE);
        transMeta.setChannelLogTable(channelLogTable);
    }

    /**
     *  转换-性能监控
     *
     * @author jianshengxiong
     * @date 2019/12/31/15:45
     */
    private static void setMetricsLogTable(TransMeta transMeta){
        MetricsLogTable metricsLogTable = MetricsLogTable.getDefault(SPACE,transMeta);
        metricsLogTable.setConnectionName(KettleConstant.KETTLE_LOG_DB);
        metricsLogTable.setTableName(KETTLE_LOG_TRANS_METRICS_TABLE);
        transMeta.setMetricsLogTable(metricsLogTable);
    }
}
